/*
 * Copyright (C) 2021 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the
 *       distribution.
 *    3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
 *       its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <stdio.h>
#include "hal_file.h"
#include "utils_file.h"
#include "los_fs.h"
// #include "los_config.h"
// #include "securec.h"

#define IndexName "//"
#define MaxOpenFile 32
#define FR_OK 0
#define File_Debug 0

#if File_Debug
#define FILE_LOG(...) printf(__VA_ARGS__)
#else
#define FILE_LOG(...)
#endif

/* 文件管理系统参数转换 */
int ReadModeChange(int oflag)
{
	int lfsOpenFlag = 0, buffer = 0;
	buffer = (oflag & 0x000f);
	if (buffer == O_RDONLY_FS) {
		lfsOpenFlag = 0x00;
	} else if (buffer == O_WRONLY_FS) {
		lfsOpenFlag = 0x01;
	} else if (buffer == O_RDWR_FS) {
		lfsOpenFlag = 0x02;
	} else {
	}

	if (oflag & O_CREAT_FS) {
		lfsOpenFlag |= 0x200;
	}

	if (oflag & O_EXCL_FS) {
		lfsOpenFlag |= 0x800;
	}

	if (oflag & O_TRUNC_FS) {
		lfsOpenFlag |= 0x400;
	}

	if (oflag & O_APPEND_FS) {
		lfsOpenFlag |= 0x8;
	}
	return lfsOpenFlag;
}

int HalFileOpen(const char *path, int oflag, int mode)
{
	char *Pathname = NULL;
	int ret = -1;
	Pathname = malloc(strlen(path) + strlen(IndexName) + 1);
	memset(Pathname, 0, sizeof(Pathname));
	strcpy(Pathname, "//");
	strcat(Pathname, path);
	ret = LOS_Open(Pathname, ReadModeChange(oflag), mode);
    if (ret >= 0) {
        ret ++;
    }
	free(Pathname);
	return ret;
}

int HalFileClose(int fd)
{
	return LOS_Close(fd - 1);
}

int HalFileRead(int fd, char *buf, unsigned int len)
{
	return LOS_Read(fd - 1, buf, len);
}

int HalFileWrite(int fd, const char *buf, unsigned int len)
{
	return LOS_Write(fd - 1, buf, len);
}

int HalFileDelete(const char *path)
{
	return LOS_Unlink(path);
}

int HalFileStat(const char *path, unsigned int *fileSize)
{
	return LOS_Fstat(path, fileSize);
}

int HalFileSeek(int fd, int offset, unsigned int whence)
{
	return LOS_Lseek(fd - 1, offset, whence);
}